﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Store.Goods
{
    public partial class Info : InfoPage
    {
        BLL.V_Goods bll = new BLL.V_Goods();
        protected Model.V_Goods model = new Model.V_Goods();
        //
        BLL.Pic bllPic = new BLL.Pic();
        BLL.Category bllCategory = new BLL.Category();
        BLL.Goods_BuyPrice_Log bllBuyPrice_Log = new BLL.Goods_BuyPrice_Log();
        BLL.Goods_SellPrice_Log bllSellPrice_Log = new BLL.Goods_SellPrice_Log();
        //
        Model.Store CurrentStore = new Model.Store();
        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentStore = Security.GetCurrentStore();
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { 
            model = bll.GetModelList("id="+Master.id)[0];
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            if (DataTypePlus.requestInt(Request.QueryString["HideOrderForm"]) == 1) group_OrderForm.Visible = false;
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
           // a_Goods_BuyPrice_Log.Visible = bllBuyPrice_Log.GetRecordCount("GoodsID=" + Master.id) > 0;
            
            rp_pic.DataSource = bllPic.GetModelList("GoodsID=" + Master.id);
            rp_pic.DataBind();
        }

        protected void bt_submit_Click(object sender, EventArgs e)
        {
            int ExpectAmount = DataTypePlus.requestInt(tb_ExpectAmount.Text);
            if (PageValidate.noZero(ExpectAmount, "至少需要订购1个"))
            {
                //Model.Orders modelOrders_WaitMoney=new Model.Orders();
                //订单ID
                int OrdersID_WaitMoney = 0;
                BLL.Orders bllOrders=new BLL.Orders();
                List<Model.Orders> listModelOrders_WaitMoney = bllOrders.GetModelList("StoreID=" + CurrentStore .id+ " and State<=" + (int)EnumHelper.OrdersState.WaitMoney);
                if (listModelOrders_WaitMoney.Count == 0)
                {
                    Model.Orders modelOrder_new = new Model.Orders();
                    modelOrder_new.StoreID = CurrentStore.id;
                    modelOrder_new.Number = DateTime.Now.Year + "" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "" + DateTime.Now.Day.ToString().PadLeft(2, '0') + CurrentStore.Number;
                    modelOrder_new.State = (int)EnumHelper.OrdersState.WaitMoney;
                    //modelOrder_new.BuyDate = DataTypePlus.WeekDay2Date(Security.GetCurrentStore().BuyDay);
                    OrdersID_WaitMoney = bllOrders.Add(modelOrder_new);
                    //Model.Orders_Stat modelOrders_Stat=new Model.Orders_Stat();
                    //string kk = modelOrders_Stat.Count.ToString();
                    //BLL.Orders_Stat.Increase(kk);
                }
                else
                {
                    OrdersID_WaitMoney = listModelOrders_WaitMoney[0].id;
                    Model.Orders modelOrders_Now = bllOrders.GetModel(OrdersID_WaitMoney);
                    if (modelOrders_Now.State == (int)EnumHelper.OrdersState.NoGoods)
                    {
                        modelOrders_Now.State = (int)EnumHelper.OrdersState.WaitMoney;
                        bllOrders.Update(modelOrders_Now);
                        BLL.User_Operate_Log.Add(CurrentStore.UserID, EnumHelper.TableName.Orders, OrdersID_WaitMoney, EnumHelper.OperateType.Update, "选择了商品");
                    }
                }
                //货品
                Model.Orders_Goods modelOrders_Goods = new Model.Orders_Goods();
                BLL.Orders_Goods bllOrders_Goods=new BLL.Orders_Goods();
                List<Model.Orders_Goods> listModelOrders_Goods = bllOrders_Goods.GetModelList("OrdersID=" + OrdersID_WaitMoney + " and GoodsID=" + Master.id + "");
                if (listModelOrders_Goods.Count == 0)
                {
                    modelOrders_Goods.OrdersID = OrdersID_WaitMoney;
                    modelOrders_Goods.GoodsID = Master.id;
                    modelOrders_Goods.ExpectAmount = ExpectAmount;
                    bllOrders_Goods.Add(modelOrders_Goods);
                }
                else
                {
                    modelOrders_Goods = listModelOrders_Goods[0];
                    modelOrders_Goods.ExpectAmount += ExpectAmount;
                    bllOrders_Goods.Update(modelOrders_Goods);
                }
                Master.ScriptManager_js(Master.ScriptManager_Alert_float_Str("成功加入订单", true) + "parent.MakeOrders_OK("+Master.id+");parent.popover_close();");
                //JsHelper.Alert_float("成功加入订单", true);
                //HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>parent.popover_close()</script>"));
            }
        }

    }
}